package com.bailong.nanny.controller;


import com.bailong.nanny.entity.Admin;
import com.bailong.nanny.service.LoginService;
import com.bailong.nanny.service.impl.LoginServiceImpl;
import com.mysql.cj.Session;
import org.apache.taglibs.standard.lang.jstl.test.PageContextImpl;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import javax.xml.ws.RequestWrapper;
import java.io.IOException;

//@WebService(name= "/LoginServlet")
@WebServlet(name= "/login.do",urlPatterns = "/login.do")
public class LoginServerlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);

    }
    private void loginInfo(String s,HttpServletResponse resp){
        resp.setContentType("text/html;charset=UTF-8");
        resp.setHeader("Location", "users.jsp");
        try {
            resp.getWriter().println("<html>");
            resp.getWriter().println("<head>");
            resp.getWriter().println("<title>登录信息</title>");
            resp.getWriter().println("</head>");
            resp.getWriter().println("<body>");
            resp.getWriter().println(s);
            resp.getWriter().println("</body>");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = (String) req.getSession().getAttribute("code");
        String subCode = req.getParameter("code");
        if(!code.equals(subCode)){
            req.setAttribute("error","验证码错误");
            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        }else {
            LoginService loginService = new LoginServiceImpl();
            Admin admin = loginService.LoginDao(username, password);
            if (admin != null) {
                if (!admin.getAdmin_name().equals(username)) {
//                loginInfo("用户名错误",resp);
                    req.setAttribute("error", "用户名错误");
                    req.getRequestDispatcher("/index.jsp").forward(req, resp);
                } else if (!admin.getAdmin_password().equals(password)) {
//                loginInfo("密码错误",resp);
                    req.setAttribute("error", "密码错误");
                    req.getRequestDispatcher("/index.jsp").forward(req, resp);
                } else {
//                loginInfo("登录城功",resp);
                    req.getSession().setAttribute("ok", admin);
//                    req.getRequestDispatcher("/user.do?m=selectAllUser").forward(req, resp);
//                    resp.sendRedirect("/user.do?m=selectAllUser");
                    resp.sendRedirect("/navigation.jsp");
                }
            } else {
                System.out.println("登录失败");
                req.setAttribute("error", "登录失败");
                req.getRequestDispatcher("/index.jsp").forward(req, resp);
//            loginInfo("登录失败",resp);
            }
        }

    }
}
